//@ts-ignore
import * as XLSX from './xlsx'

export default (): Promise<Array<any>> => {
  return new Promise<Array<any>>((resovle, reject) => {
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      extension: ['xls', 'xlsx'],
    }).then(res => {
      let tempFiles = res.tempFiles;
      if (tempFiles.length === 0) {
        return resovle([]);
      }
      let tempFile = tempFiles[0];
      let fs = wx.getFileSystemManager();
      let fd = fs.openSync({
        filePath: tempFile.path
      });
      let stat = fs.fstatSync({
        fd: fd
      });
      let size = stat.size;
      let ab = new ArrayBuffer(size);
      let result = fs.readSync({
        fd: fd,
        arrayBuffer: ab,
        position: 0,
        length: size
      });
      let arrayBuffer = result.arrayBuffer;
      fs.closeSync({
        fd: fd
      });
      let u8a = new Uint8Array(arrayBuffer);
      let workbook = XLSX.read(u8a, { type: 'array' });
      let sheets = workbook.Sheets;
      let sheet = sheets[workbook.SheetNames[0]];
      let range = XLSX.utils.decode_range(sheet['!ref']);
      let rowCount = range.e.r + 1;
      let columnCount = range.e.c + 1;
      if (rowCount < 1) {
        reject('表格格式错误');
        return;
      }
      if (columnCount < 5) {
        reject('表格格式错误');
        return;
      }
      let getSheetValue = (row: number, column: number): string => {
        let addr = XLSX.utils.encode_cell({ r: row, c: column });
        let cell = sheet[addr];
        if (cell && cell.w) {
          return cell.w;
        }
        return '';
      }

      let trimedRowCount = rowCount;
      for (let i = 3; i < rowCount; i++) {
        let sampleCode = getSheetValue(i, 0);
        if (!sampleCode) {
          trimedRowCount = i;
          break;
        }
      }

      rowCount = trimedRowCount;

      if (getSheetValue(0, 0) !== '检测项目') {
        reject('表格格式错误');
        return;
      }
      if (getSheetValue(0, 1) !== '检测项目ID') {
        reject('表格格式错误');
        return;
      }
      if (getSheetValue(0, 2) !== '样本类型') {
        reject('表格格式错误');
        return;
      }
      if (getSheetValue(0, 3) !== '样本编号') {
        reject('表格格式错误');
        return;
      }
      if (getSheetValue(0, 4) !== '姓名') {
        reject('表格格式错误');
        return;
      }
      if (getSheetValue(0, 5) !== '性别') {
        reject('表格格式错误');
        return;
      }
      if (getSheetValue(0, 6) !== '手机号') {
        reject('表格格式错误');
        return;
      }
      if (getSheetValue(0, 7) !== '出生日期') {
        reject('表格格式错误');
        return;
      }
      if (getSheetValue(0, 8) !== '备注') {
        reject('表格格式错误');
        return;
      }

      let array = new Array<any>();
      for (let i = 1; i < rowCount; i++) {
        let gender = getSheetValue(i, 5);
        if (gender === '男') {
          gender = 'MALE';
        } else if (gender === '女') {
          gender = "FEMALE";
        } else {
          reject('表格格式错误');
          return;
        }
        let data = {
          testingItemName: getSheetValue(i, 0),
          testingItemId: getSheetValue(i, 1),
          sampleTypeId: 0,
          sampleTypeName: getSheetValue(i, 2),
          code: getSheetValue(i, 3),
          name: getSheetValue(i, 4),
          gender: gender,
          phone: getSheetValue(i, 6),
          birthday: getSheetValue(i, 7),
          remark: getSheetValue(i, 8),
          extraDataList: new Array<any>(0)
        };
        array.push(data);
      }
      resovle(array);
    }).catch(e => {
      reject(e);
    })
  })
}